from PySide2.QtWidgets import QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QMessageBox

from business.business2 import Calculator
class Page4(QWidget):
    def __init__(self):
        super().__init__()
        self.phi = 0.0
        self.sin = 0.0

        labels = [
            "真厚度", "地层倾角", "本井特征点垂深", "平面距离", "预测垂深"
        ]

        self.input_field_1 = QLineEdit()
        self.input_field_2 = QLineEdit()
        self.input_field_3 = QLineEdit()
        self.input_field_4 = QLineEdit()
        self.output_field_5 = QLineEdit()

        self.output_field_5.setReadOnly(True)

        main_layout = QVBoxLayout()

        for i, label_text in enumerate(labels):
            h_layout = QHBoxLayout()
            label = QLabel(label_text)
            h_layout.addWidget(label)
            if i < 4:
                h_layout.addWidget(getattr(self, f'input_field_{i+1}'))
            else:
                h_layout.addWidget(self.output_field_5)
            main_layout.addLayout(h_layout)

        button_layout = QHBoxLayout()
        confirm_button = QPushButton("确定")
        reset_button = QPushButton("重置")
        button_layout.addWidget(confirm_button)
        button_layout.addWidget(reset_button)
        main_layout.addLayout(button_layout)
        self.setLayout(main_layout)

        confirm_button.clicked.connect(self.on_confirm)
        reset_button.clicked.connect(self.on_reset)

    def on_confirm(self):
        fields = [self.input_field_1, self.input_field_2, self.input_field_3, self.input_field_4]
        inputs = []
        for field in fields:
            text = field.text().strip()
            if not text:
                QMessageBox.critical(self, "错误", "输入不能为空")
                return
            try:
                value = float(text)
                inputs.append(value)
            except ValueError:
                QMessageBox.critical(self, "错误", "请输入有效数字")
                return

        vertical_depth_phi = Calculator().calculate_vertical_depth_phi(inputs[0], inputs[1])
        vertical_depth_sin = Calculator().calculate_vertical_depth_sin(inputs[3], inputs[1])
        result = round(Calculator().calculate_down_vertical_depth(inputs[2], vertical_depth_phi, vertical_depth_sin), 1)
        
        self.output_field_5.setText(str(result))

    def on_reset(self):
        for i in range(1,5):
            getattr(self, f'input_field_{i}').clear()
        self.output_field_5.clear()  # 明确保留输出框清空
